//
//  CartView.m
//  AppleCilent
//
//  Created by Nail Sharipov on 06/04/14.
//  Copyright (c) 2014 Nail Sharipov. All rights reserved.
//

#import "CartTableView.h"
#import "CartTableViewCell.h"
#import "CartManager.h"
#import "GoodsDetailsViewController.h"
#import "SceneManager.h"
#import "ResourceData.h"
#import <QuartzCore/QuartzCore.h>

@implementation CartTableView
{
    NSArray* orderList;
    UITableView* cartTableView;
    UILabel* total;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    
    return self;
}


- (id)initWithCoder:(NSCoder*)coder
{
    if ((self = [super initWithCoder:coder])) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    CGRect rect = [self frame];
    float cellHeight = [CartTableViewCell getHeight];
    rect.size.height -= cellHeight;
    rect.origin.x = 0.0f;
    rect.origin.y = 0.0f;
    
    cartTableView = [[UITableView alloc] initWithFrame:rect];
    cartTableView.dataSource = self;
    cartTableView.delegate = self;
    [cartTableView setEditing:NO animated:NO];
    [self addSubview:cartTableView];
    
    rect.origin.y = rect.size.height;
    rect.size.height = cellHeight;

    UILabel* totalTitle = [[UILabel alloc] initWithFrame:rect];
    [totalTitle setTextColor: [UIColor whiteColor]];
    [totalTitle setAdjustsFontSizeToFitWidth:NO];
    [totalTitle setAdjustsLetterSpacingToFitWidth:NO];
    [totalTitle setTextAlignment:NSTextAlignmentLeft];
    [totalTitle setFont: [UIFont boldSystemFontOfSize: 16.0f]];
    [totalTitle setText:@"  Итого:"];

    [totalTitle setBackgroundColor:[UIColor colorWithWhite:0.2f alpha:1.0f]];
    [self addSubview:totalTitle];

    rect.origin.x = rect.size.width - 120.0f;
    rect.size.width = 120.0f;
    
    total = [[UILabel alloc] initWithFrame:rect];
    [total setTextColor: [UIColor whiteColor]];
    [total setAdjustsFontSizeToFitWidth:NO];
    [total setAdjustsLetterSpacingToFitWidth:NO];
    [total setTextAlignment:NSTextAlignmentRight];
    [total setFont: [UIFont boldSystemFontOfSize: 16.0f]];
    [total setBackgroundColor:[UIColor clearColor]];
    [self addSubview:total];
    
    CALayer* layer = [self  layer];
    [layer setCornerRadius:10.0f];
    [layer setMasksToBounds:YES];
    [layer setBorderColor:[UIColor blackColor].CGColor];
    [layer setBorderWidth:1.0f];
}

- (void) reloadCart
{
    
    CartManager* cartManager = [CartManager getInstance];
    
    int totalCost = (int)[cartManager getTotalCost];
    [total setText:[NSString stringWithFormat:@"%i р.  ", totalCost]];
    
    orderList = [cartManager getOrderList];
    [cartTableView reloadData];
}

-(UITableViewCell*)tableView: (UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cellId";
    
    CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if(cell == nil) {
        cell = [[CartTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    if (indexPath.row < [orderList count]) {
        CartOrder* order = (CartOrder*)[orderList objectAtIndex:indexPath.row];
        [cell setOrder:order];
    }
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    return cell;
}

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
    CartOrder* order = (CartOrder*)[orderList objectAtIndex:indexPath.row];
    GoodsDomain* goods = [order getGoods];
    
    GoodsDetailsViewController* viewController = (GoodsDetailsViewController*)[[SceneManager getInstance] openScene: @"GoodsDetails"];
    
    [viewController setGoods:goods];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [CartTableViewCell getHeight];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [orderList count];
}

- (void) removeRow:(UITableViewCell*) cell
{
    NSIndexPath* pathOfTheCell = [cartTableView indexPathForCell:cell];

    CartManager* cartManager = [CartManager getInstance];
    
    CartOrder* order = (CartOrder*)[orderList objectAtIndex:pathOfTheCell.row];
    [order setQuantity:0];
    
    orderList = [cartManager getOrderList];
    
    [cartTableView beginUpdates];
    [cartTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:pathOfTheCell, nil] withRowAnimation:UITableViewRowAnimationFade];
    [cartTableView endUpdates];
    
    int totalCost = (int)[cartManager getTotalCost];
    [total setText:[NSString stringWithFormat:@"%i р.  ", totalCost]];
}

@end
